package com.iplant.base.po;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.join.multi.DBJoinMulti;
import com.iplant.base.utils.annotation.po.DBBasePO;
import com.iplant.base.utils.annotation.condition.DBCondition;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.base.utils.general.StringUtils;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Map;


/**
 * @author xiuto
 */
@DBBasePO(TableName = "bms_basepo", Code = false, CheckName = {"Code"}, CodeAuto = true, CodePrefix = "BasePO")
public class BasePO implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @DBCondition
    @DBField(Insert = false)
    public int ID;
    @DBCondition(CompareType = DBCondition.DBCompareType.like,ParamName2 = "NameEQ")
    @DBField
    public String Name = "";
    @DBCondition(CompareType = DBCondition.DBCompareType.like,ParamName2 = "CodeEQ")
    @DBField
    public String Code = "";
    @DBCondition(CompareType = DBCondition.DBCompareType.geLe, ParamName = "CStartTime", ParamName2 = "CEndTime")
    @DBField(Update = false)
    public Calendar CreateTime = Calendar.getInstance();

    @DBCondition
    @DBField(Update = false)
    public int CreatorID = 0;

    @DBCondition(CompareType = DBCondition.DBCompareType.geLe, ParamName = "DStartTime", ParamName2 = "DEndTime")
    @DBField
    public Calendar EditTime = Calendar.getInstance();

    @DBCondition
    @DBField
    public int EditorID = 0;

    @DBJoinMulti(Name="Name",TableName ="mbs_user", Schemas = DBSchemas.Basic, MainTableField =
            "CreatorID")
    public String CreatorName = "";

    @DBJoinMulti(Name="Name",TableName ="mbs_user", Schemas = DBSchemas.Basic, MainTableField =
            "EditorID")
    public String EditorName = "";

    /**
     * 状态
     */
    @DBCondition
    @DBField
    public int Active = 0;

    /**
     * 描述
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like,ParamName2 = "DescriptionEQ")
    @DBField
    public String Description = "";

    public BasePO() {
        ID = 0;
    }

    public void setParams(Map<String, Object> wParams) {

        if (StringUtils.isNotEmpty(this.Name)) {
            wParams.put("Name", this.Name);
        }
        if (StringUtils.isNotEmpty(this.Code)) {
            wParams.put("Code", this.Code);
        }
        if (StringUtils.isNotEmpty(this.Description)) {
            wParams.put("Description", this.Description);
        }
        wParams.put("EditorID", this.EditorID);
        wParams.put("EditTime", Calendar.getInstance());
        wParams.put("Active", this.Active);
        if (this.ID > 0) {
            wParams.put("ID", this.ID);
        } else {
            wParams.put("CreatorID", this.CreatorID);
            wParams.put("CreateTime", Calendar.getInstance());
        }

    }

    public void setReaders(Map<String, Object> wReader) {

        if (wReader.containsKey("ID")) {
            this.ID = StringUtils.parseInt(wReader.get("ID"));
        }
        if (wReader.containsKey("Name")) {
            this.Name = StringUtils.parseString(wReader.get("Name"));
        }
        if (wReader.containsKey("Code")) {
            this.Code = StringUtils.parseString(wReader.get("Code"));
        }
        if (wReader.containsKey("Description")) {
            this.Description = StringUtils.parseString(wReader.get("Description"));
        }
        if (wReader.containsKey("Active")) {
            this.Active = StringUtils.parseInt(wReader.get("Active"));
        }
        if (wReader.containsKey("CreatorID")) {
            this.CreatorID = StringUtils.parseInt(wReader.get("CreatorID"));
        }
        if (wReader.containsKey("EditorID")) {
            this.EditorID = StringUtils.parseInt(wReader.get("EditorID"));
        }
        if (wReader.containsKey("CreatorName")) {
            this.CreatorName = StringUtils.parseString(wReader.get("CreatorName"));
        }
        if (wReader.containsKey("EditorName")) {
            this.EditorName = StringUtils.parseString(wReader.get("EditorName"));
        }
        if (wReader.containsKey("CreateTime")) {
            this.CreateTime = StringUtils.parseCalendar(wReader.get("CreateTime"));
        }
        if (!wReader.containsKey("EditTime")) {
            this.EditTime = StringUtils.parseCalendar(wReader.get("EditTime"));
        }

    }


    public void setUserInfo(BaseUser wBaseUser) {

        this.EditorName = wBaseUser.getName();
        this.EditorID = wBaseUser.getID();
        this.EditTime = Calendar.getInstance();


        if (this.ID <= 0) {

            this.CreatorName = wBaseUser.getName();
            this.CreatorID = wBaseUser.getID();
            this.CreateTime = Calendar.getInstance();
        }
    }


    @JsonProperty("ID")
    public int getID() {
        return ID;
    }

    public void setID(int iD) {
        ID = iD;
    }

    @JsonProperty("Name")
    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    @JsonProperty("Code")
    public String getCode() {
        return Code;
    }

    public void setCode(String code) {
        Code = code;
    }

    @JsonProperty("CreateTime")
    public Calendar getCreateTime() {
        return CreateTime;
    }

    public void setCreateTime(Calendar createTime) {
        CreateTime = createTime;
    }

    @JsonProperty("CreatorID")
    public int getCreatorID() {
        return CreatorID;
    }

    public void setCreatorID(int creatorID) {
        CreatorID = creatorID;
    }

    @JsonProperty("EditTime")
    public Calendar getEditTime() {
        return EditTime;
    }

    public void setEditTime(Calendar editTime) {
        EditTime = editTime;
    }

    @JsonProperty("EditorID")
    public int getEditorID() {
        return EditorID;
    }

    public void setEditorID(int editorID) {
        EditorID = editorID;
    }

    @JsonProperty("CreatorName")
    public String getCreatorName() {
        return CreatorName;
    }

    public void setCreatorName(String creatorName) {
        CreatorName = creatorName;
    }

    @JsonProperty("EditorName")
    public String getEditorName() {
        return EditorName;
    }

    public void setEditorName(String editorName) {
        EditorName = editorName;
    }

    @JsonProperty("Active")
    public int getActive() {
        return Active;
    }

    public void setActive(int active) {
        Active = active;
    }

    @JsonProperty("Description")
    public String getDescription() {
        return Description;
    }

    public void setDescription(String description) {
        Description = description;
    }


}
